import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../views/login/Login.vue'
import Layout from '../views/layout/Layout.vue'


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    // 登录页面
    path: '/login',
    component: Login
  },
  {
    // 大布局
    path: '/layout',
    component: Layout
  },
  // 后台首页模块
  {
    path: '/',
    component: Layout,
    redirect: "/home",
    children: [
      {
        path: '/home',
        component: () => import('../views/home/Home.vue'),
      },
    ]
  },
  // 订单管理模块
  {
    path: '/order',
    component: Layout,
    redirect: "/order",
    meta: {
      path: "/order",
      title: "订单管理"
    },
    children: [
      {
        path: '/order',
        component: () => import('../views/order/Order.vue'),
      },
    ]
  },
  // 商品管理模块
  {
    path: '/goods',
    component: Layout,
    redirect: "/goods-list",
    meta: {
      path: "/goods",
      title: "商品管理"
    },
    children: [
      {
        path: '/goods-list',
        meta: {
          path: "/goods-list",
          title: "商品列表"
        },
        component: () => import('../views/goods/GoodsList.vue'),
      },
      {
        path: '/goods-add',
        meta: {
          path: "/goods-add",
          title: "商品添加"
        },
        component: () => import('../views/goods/GoodsAdd.vue'),
      },
      {
        path: '/goods-type',
        meta: {
          path: "/goods-type",
          title: "商品分类"
        },
        component: () => import('../views/goods/GoodsType.vue'),
      },
    ]
  },
  // 店铺管理模块
  {
    path: '/shop',
    component: Layout,
    redirect: "/shop",
    children: [
      {
        path: '/shop',
        meta: {
          path: "/shop",
          title: "店铺管理"
        },
        component: () => import('../views/shop/Shop.vue'),
      },
    ]
  },
  // 账号管理模块
  {
    path: '/account',
    component: Layout,
    meta: {
      path: "/account",
      title: "账号管理"
    },
    redirect: "/account-list",
    children: [
      {
        path: '/account-list',
        meta: {
          path: "/account-list",
          title: "账号列表"
        },
        component: () => import('../views/account/AccountList.vue'),
      },
      {
        path: '/account-add',
        meta: {
          path: "/account-add",
          title: "添加账号"
        },
        component: () => import('../views/account/AccountAdd.vue'),
      },
      {
        path: '/account-edit',
        meta: {
          path: "/account-edit",
          title: "修改密码"
        },
        component: () => import('../views/account/AccountEdit.vue'),
      },
      {
        path: '/account-center',
        meta: {
          path: "/account-center",
          title: "个人中心"
        },
        component: () => import('../views/account/AccountCenter.vue'),
      },
    ]
  },
  // 统计模块
  {
    path: '/statistics',
    component: Layout,
    meta: {
      path: "/statistics",
      title: "销售统计"
    },
    redirect: "/goods-statistics",
    children: [
      {
        path: '/goods-statistics',
        meta: {
          path: "/goods-statistics",
          title: "商品统计"
        },
        component: () => import('../views/statistics/GoodsStatistics.vue'),
      },
      {
        path: '/order-statistics',
        meta: {
          path: "/order-statistics",
          title: "订单统计"
        },
        component: () => import('../views/statistics/OrderStatistics.vue'),
      },
    ]
  },

]



const router = new VueRouter({
  routes,
});



// 路由全局守卫------------------------------------
router.beforeEach((to, from, next) => {
  // 如果用户访问的是 /login就直接跳转到login
  if(to.path==='/login'){
    next()
  };
  // 如果用户不是访问的login，
  if (localStorage.getItem('user')) {
    // 如果有用户信息，就直接随便跳转到哪
    next();
  } else {
    // 否则就强制跳转到登录页
    next('/login');
  }
})




export default router
